package com.market.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.entity.GoodsType;
import com.market.service.GoodsTypeService;
import com.market.vo.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/GoodsType")
public class GoodsTypeController {
    @Resource
    public GoodsTypeService goodsTypeService;

    //1.新增商品类型
    @PostMapping("/addGoodsType")
    public R<GoodsType> addGoodType(@RequestBody GoodsType goodsType){
        if (goodsTypeService.addGoodsType(goodsType.getTypeName())){
            return new R(200, "新增商品类型成功",goodsType.getTypeName());
        }else {
            return new R<>(300,"新增商品类型失败");
        }
    }

    //2.通过商品类别id修改商品类型名
    @GetMapping("/updateGoodsTypeName")
    public R<GoodsType>updateGoodsTypeName(int typeId,String typeName){
        if (goodsTypeService.updateGoodsTypeName(typeId,typeName)){
            return new R<>(200,"修改商品类型名成功");
        }else {
            return new R<>(300,"修改商品类型名失败");
        }
    }

    //查询全部商品类别
    @GetMapping("/queryTotalGoodsType")
    public R queryTotalGoodsType(int currentPage,int pageSize){
        PageHelper.startPage(currentPage, pageSize);
        List<GoodsType> goodsTypes = goodsTypeService.queryTotalGoodsType();
        if (goodsTypes!=null){
            return new R<>(200,"查询全部商品类型成功",new PageInfo<>(goodsTypes));
        }else {
            return new R<>(300,"查询全部商品类型失败");
        }
    }

    @RequestMapping("queryAllType")
    public R queryAllType(){
        List<GoodsType> goodsTypes = goodsTypeService.queryTotalGoodsType();
        return new R(200,"查询分类成功",goodsTypes);
    }
}
